"""
Case Type   : 资源池化
Case Name   : 资源池化场景下, 无任何业务压力, 进行failover操作(kill 一次)
Create At   : 2024-11-22
Owner       : hejiahuan
Description :
    1、查看集群状态 cm_ctl query -Cv
    2、构建failover(kill 一次)操作
    3、查看构建后集群状态 cm_ctl query -Cv
    4、恢复集群原始状态
Expect      :
    1、集群状态正常, 数据库节点状态正常
    2、构建failover(kill 一次)操作成功
    3、构建十秒后集群状态正常, 数据库节点状态正常
    4、恢复集群原始状态成功,集群状态正常
History     :
    1、2024-11-22 创建
"""

import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')
Standby1_SH = CommonSH('Standby1DbUser')
Standby2_SH = CommonSH('Standby2DbUser')

@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')
        self.s2_comsh = CommonSH('Standby2DbUser')

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:主节点构建(kill 一次); expect:构建成功-----'
        self.log.info(text)
        # 运行kill脚本进行failover(kill 一次)构建
        stopdn = Primary_SH.exec_kill()
        self.log.info(stopdn)
        self.assertIn('kill done', stopdn, '执行失败' + text)

        text = '-----step3:十秒后查询集群状态; expect:集群状态正常-----'
        self.log.info(text)
        time.sleep(10)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        res1 = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        res2 = status.splitlines()[-1].split('|')[1].split('6002')[-1]
        res3 = status.splitlines()[-1].split('|')[2].split('6003')[-1]
        self.log.info(res1)
        self.log.info(res2)
        self.log.info(res3)
        self.assertIn('P Primary Normal', res1, '执行失败' + text)
        self.assertIn('S Standby Normal', res2, '执行失败' + text)
        self.assertIn('S Standby Normal', res3,'执行失败' + text)

    def tearDown(self):
        text = '-----step4:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('successfully', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('successfully', status, '执行失败' + text)

        recovery_switchover = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(recovery_switchover)
        self.assertIn(self.constant.cm_switchover_success_msg, recovery_switchover,
                      '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                    status.splitlines()[-1].split('|')[2].split('6003')[-1],
                    '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
